﻿using System.Collections.Generic;

namespace LuatChoi
{
    public abstract class QuanCo
    {
        public Phe Mau;
        public Vector ToaDo;

        public List<Vector> ToaDoCoTheDiChuyen;
        public List<Vector> ToaDoCoTheTanCong;
        public TrangThaiQuan TrangThai;
        public BanCo Ban;

        protected QuanCo()
        {
            Mau = Phe.ChuaXacDinh;
            TrangThai = TrangThaiQuan.ChuaDi;
            ToaDo = new Vector();
        }

        protected QuanCo(BanCo ban)
            : this()
        {
            Ban = ban;
        }

        //kiểm tra xem quân này có thể phong thành quân khác hay không
        public bool CoThePhong
        {
            get
            {
                return (this is Tot) &&
                       ((Mau == Phe.Trang && ToaDo.Y == 7) || (Mau == Phe.Trang && ToaDo.Y == 7));
            }
        }

        public abstract void TinhToaDoAnhHuong();

        protected void CheoLenPhai(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int x = ToaDo.X + i;
                int y = ToaDo.Y + i;
                if (x > 7 || y > 7)
                    break;
                if (Ban[x, y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(x, y));
                }
                else
                {
                    if (!Mau.Equals(Ban[x, y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(x, y));
                    break;
                }
            }
        }

        protected void CheoLenTrai(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int x = ToaDo.X - i;
                int y = ToaDo.Y + i;
                if (x < 0 || y > 7)
                    break;
                if (Ban[x, y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(x, y));
                }
                else
                {
                    if (!Mau.Equals(Ban[x, y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(x, y));
                    break;
                }
            }
        }


        protected void CheoXuongPhai(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int x = ToaDo.X + i;
                int y = ToaDo.Y - i;
                if (x > 7 || y < 0)
                    break;
                if (Ban[x, y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(x, y));
                }
                else
                {
                    if (!Mau.Equals(Ban[x, y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(x, y));
                    break;
                }
            }
        }

        //chéo xuống trái
        protected void CheoXuongTrai(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int x = ToaDo.X - i;
                int y = ToaDo.Y - i;
                if (x < 0 || y < 0)
                    break;
                if (Ban[x, y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(x, y));
                }
                else
                {
                    if (!Mau.Equals(Ban[x, y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(x, y));
                    break;
                }
            }
        }

        //lên
        protected void DiLen(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int y = ToaDo.Y + i;
                if (y > 7)
                    break;
                if (Ban[ToaDo.X, y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(ToaDo.X, y));
                }
                else
                {
                    if (!Mau.Equals(Ban[ToaDo.X, y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(ToaDo.X, y));
                    break;
                }
            }
        }

        protected void DiXuong(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int y = ToaDo.Y - i;
                if (y < 0)
                    break;
                if (Ban[ToaDo.X, y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(ToaDo.X, y));
                }
                else
                {
                    if (!Mau.Equals(Ban[ToaDo.X, y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(ToaDo.X, y));
                    break;
                }
            }
        }

        protected void DiTrai(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int x = ToaDo.X - i;
                if (x < 0)
                    break;
                if (Ban[x, ToaDo.Y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(x, ToaDo.Y));
                }
                else
                {
                    if (!Mau.Equals(Ban[x, ToaDo.Y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(x, ToaDo.Y));
                    break;
                }
            }
        }

        //phải
        protected void DiPhai(int n = 7, bool xetDiChuyen = true, bool xetTanCong = true)
        {
            for (int i = 1; i <= n; i++)
            {
                int x = ToaDo.X + i;
                if (x > 7)
                    break;
                if (Ban[x, ToaDo.Y] == null)
                {
                    if (xetDiChuyen)
                        ToaDoCoTheDiChuyen.Add(new Vector(x, ToaDo.Y));
                }
                else
                {
                    if (!Mau.Equals(Ban[x, ToaDo.Y].Mau))
                        if (xetTanCong)
                            ToaDoCoTheTanCong.Add(new Vector(x, ToaDo.Y));
                    break;
                }
            }
        }


        public void DiChuyen(Vector dich)
        {
            ToaDo.X = dich.X;
            ToaDo.Y = dich.Y;
            TrangThai = TrangThaiQuan.BinhThuong;
        }
    }
}